package com.base.cn.platform.os.service.user.user;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.rsa.WebRsaUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.sms.SmsSendUtils;
import com.base.cn.platform.os.common.utils.user.UserExpandFrom;
import com.base.cn.platform.os.common.utils.user.UserUtils;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.service.manage.config.SysConfigBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证用户数据
 *
 * @author s.li
 * @create 2018-06-01-16:29
 */
@Service
public class ValidateUser {
    @Autowired
    private SysConfigBiz sysConfigBiz;
    @Autowired
    private CusUserBiz cusUserBiz;
    @Autowired
    private SmsSendUtils smsSendUtils;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 验证用户对象数据
     *
     * @param user 用户属性对象
     * @return Map<String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Object>
     */
    public Map<String, Object> vUserData(CusUser user) throws Exception{
        //如果是内购则不作验证
        if (user.getRegisterFrom().equals(UserExpandFrom.ios.getKey())) {
            return null;
        }
        //验证邮箱号
        if (StringUtils.isNotEmpty(user.getEmail())) {
            if (!UserUtils.isEmail(user.getEmail().trim())) {
                return ResultUtil.ERROR("邮箱号格式不正确");
            }
            if (ObjectUtils.isNotEmpty(cusUserBiz.queryUserByEmail(user.getEmail().trim()))) {
                return  ResultUtil.ERROR("邮箱号已经被占用");
            }
            user.setEmailIsavalible(1);
        }

        //验证手机号
        if(StringUtils.isEmpty(user.getMobile())){
            return ResultUtil.ERROR("请填写手机号");
        }else{
            if(!StringUtils.isMobile(user.getMobile())){
                return ResultUtil.ERROR("手机号格式不正确");
            }
            if (ObjectUtils.isNotEmpty(cusUserBiz.queryUserByMobile(user.getMobile().trim()))) {
                return ResultUtil.ERROR("手机号已被使用");
            }
            user.setMobileIsavalible(1);
        }

        //验证账号
        if (StringUtils.isNotEmpty(user.getLoginAccount()) && !user.getRegisterFrom().equals(UserExpandFrom.ios.getKey())) {
            if (!StringUtils.isLetterOrNum(user.getLoginAccount())) {
                return ResultUtil.ERROR("账号错误，账号必须是英文与数字组合！");
            } else {
                if(user.getLoginAccount().trim().length()< 6 || user.getLoginAccount().trim().length()>20){
                    return ResultUtil.ERROR("账号错误，长度必须在6-20位");
                }
                if (ObjectUtils.isNotEmpty(cusUserBiz.queryUserByLoginAccount(user.getLoginAccount().trim()))) {
                    return ResultUtil.ERROR("该账号已被使用");
                }
            }
        }

        //如果是前台注册
        if (user.getRegisterFrom().equals(UserExpandFrom.registerFrom.getKey()) ||
                user.getRegisterFrom().equals(UserExpandFrom.iosFrom.getKey()) ||
                user.getRegisterFrom().equals(UserExpandFrom.androidFrom.getKey()) ||
                user.getRegisterFrom().equals(UserExpandFrom.mobileFrom.getKey())) {
            //解密密码
            String password = WebRsaUtils.decrypt(customCacheChannel,user.getSign(),user.getPassword(),false);
            user.setPassword(password);
            Map<String,Object> result = validatePwd(password);
            if(result !=null){
                return result;
            }
            String confirmPwd = WebRsaUtils.decrypt(customCacheChannel,user.getSign(),user.getConfirmPwd(),true);
            user.setConfirmPwd(confirmPwd);
            if (!password.equals(confirmPwd)) {
                return ResultUtil.ERROR("两次密码不一致");
            }
            if(StringUtils.isEmpty(user.getMobileCode())){
                return ResultUtil.ERROR("请填写验证码");
            }

            TreeMap<String,String> configMap = sysConfigBiz.getSysConfigContext(ConfigName.SMS);
            smsSendUtils.setSmsConfig(configMap);
            boolean isOk = smsSendUtils.validateRegCode(user.getMobile(),user.getMobileCode());
            if(!isOk){
                return ResultUtil.ERROR("验证码错误");
            }
            //设置用户密码
            user.setPassword(password);
        }
        //如果是第三方登录注册
        if (user.getRegisterFrom().equals(UserExpandFrom.pcThirdQQ.getKey()) ||
                user.getRegisterFrom().equals(UserExpandFrom.pcThirdWeiXin.getKey()) ||
                user.getRegisterFrom().equals(UserExpandFrom.mobileThirdQQ.getKey())||
                user.getRegisterFrom().equals(UserExpandFrom.mobileThirdWeiXin.getKey())) {
            if(StringUtils.isEmpty(user.getMobileCode())){
                return ResultUtil.ERROR("请填写验证码");
            }
            TreeMap<String,String> configMap = sysConfigBiz.getSysConfigContext(ConfigName.SMS);
            smsSendUtils.setSmsConfig(configMap);
            boolean isOk = smsSendUtils.validateRegCode(user.getMobile(),user.getMobileCode());
            if(!isOk){
                return ResultUtil.ERROR("验证码错误");
            }
        }
        return null;
    }

    /**
     * 验证密码
     * @param password
     * @return
     */
    public Map<String,Object> validatePwd(String password){
        if (StringUtils.isEmpty(password)) {
            return  ResultUtil.ERROR("请输入密码");
        }
        //密码正则表达式
        Pattern pattern = Pattern.compile("^[a-zA-Z0-9-!@#$%^&*?(){}+\\-=_/\\\\,.<>\\]\\[\\|;'\\:]+$");
        Matcher matcher = pattern.matcher(password);
        if (!matcher.matches()) {
            return ResultUtil.ERROR("密码仅支持大小写英文字母和数字及 !@#$%^&*?( ) _ +-=/\\,.{ }< > | ;':[ ] 字符");
        }
        int len = password.trim().length();
        if (len < 6 || len > 20) {
            return ResultUtil.ERROR("密码长度6-20位");
        }
        return null;
    }
}
